package com.library.weidget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.R;
import com.library.utils.RegexUtils;
import com.library.utils.StringUtils;

/**
 * Created by Administrator on 2016/1/15.
 */
public class CustomeTipCon extends RelativeLayout implements View.OnClickListener {
    public interface tipChangedLisener {
        void tipChanged(String tip);
    }

    private LinearLayout ll_tip_con;
    private TextView tv_tip1, tv_tip2, tv_tip3, tv_tip4;
    private TextView tv_show;
    private EditText et_input;

    private tipChangedLisener lisener;

    public void setLisener(tipChangedLisener lisener) {
        this.lisener = lisener;
    }

    public CustomeTipCon(Context context) {
        super(context);
        initView(context);
    }

    public CustomeTipCon(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CustomeTipCon(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custome_tip_choose, this);
        ll_tip_con = (LinearLayout) findViewById(R.id.tip_choose_con);
        tv_show = (TextView) findViewById(R.id.tip_choose_tip);
        tv_tip1 = (TextView) findViewById(R.id.tip_choose_tip1);
        tv_tip2 = (TextView) findViewById(R.id.tip_choose_tip2);
        tv_tip3 = (TextView) findViewById(R.id.tip_choose_tip3);
        tv_tip4 = (TextView) findViewById(R.id.tip_choose_tip4);
        et_input = (EditText) findViewById(R.id.tip_choose_tip_input);


        tv_show.setOnClickListener(this);
        tv_tip1.setOnClickListener(this);
        tv_tip2.setOnClickListener(this);
        tv_tip3.setOnClickListener(this);
        tv_tip4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tip_choose_tip:
                chooseTip();
                break;
            case R.id.tip_choose_tip1:
                tipSelected(tv_tip1.getText().toString().trim());
                break;
            case R.id.tip_choose_tip2:
                tipSelected(tv_tip2.getText().toString().trim());
                break;
            case R.id.tip_choose_tip3:
                tipSelected(tv_tip3.getText().toString().trim());
                break;
            case R.id.tip_choose_tip4:
                tipSelected(tv_tip4.getText().toString().trim());
                break;
        }
    }

    private void tipSelected(String tip) {
        ll_tip_con.setVisibility(INVISIBLE);
        tv_show.setVisibility(VISIBLE);
        tv_show.setText(tip);
        tv_show.setSelected(true);

        ObjectAnimator.ofFloat(tv_show, ALPHA, 0.5f, 1.0f).setDuration(800).start();

        if (tip != null) {
            String relTip = RegexUtils.getNumStr(tip);
            if (!StringUtils.isEmpty(relTip) && lisener != null)
                lisener.tipChanged(relTip);
        }
    }

    private void chooseTip() {
        ll_tip_con.setVisibility(VISIBLE);
        tv_show.setVisibility(INVISIBLE);

        ObjectAnimator.ofFloat(ll_tip_con, TRANSLATION_X, 200.0f, 0f).setDuration(800).start();

    }
}
